//
//  MQComposeToolBar.swift
//  SinaWeiBo
//
//  Created by 毛强 on 16/1/31.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
/**
 toolBar的按钮tag类型
 
 - Picture:  <#Picture description#>
 - mention:  <#mention description#>
 - trend:    <#trend description#>
 - emoticon: <#emoticon description#>
 - Add:      <#Add description#>
 */
enum MQComposeToolBarButtonTagType:Int{
    case Picture = 1000
    case Mention = 1001
    case Trend = 1002
    case Emoticon = 1003
    case Add = 1004
}

@available(iOS 9.0, *)
class MQComposeToolBar: UIStackView {
    
    var buttonClickClosure: ((btnTagType:MQComposeToolBarButtonTagType)->())?
    var isSystemKeyBoard:Bool = true{
        didSet{
            let emoticonBtn = viewWithTag(MQComposeToolBarButtonTagType.Emoticon.rawValue) as! UIButton
            var imageName = "compose_keyboardbutton_background"
            if isSystemKeyBoard{
                imageName = "compose_emoticonbutton_background"
            }
            emoticonBtn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
            emoticonBtn.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    private func setupUI(){
        //添加子控件
        addChildBtn("compose_toolbar_picture", btnTag: .Picture)
        addChildBtn("compose_mentionbutton_background", btnTag: .Mention)
        addChildBtn("compose_trendbutton_background", btnTag:  .Trend)
        addChildBtn("compose_emoticonbutton_background", btnTag: .Emoticon)
        addChildBtn("compose_add_background", btnTag: .Add)
        
    }
    private func addChildBtn(imageName:String, btnTag:MQComposeToolBarButtonTagType){
        let btn = UIButton()
        
        btn.addTarget(self, action: "childBtnClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.tag = btnTag.rawValue
        distribution = .FillEqually
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
//        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        btn.backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)
        addArrangedSubview(btn)
    }
    
    @objc private func childBtnClick(btn:UIButton){
//        printfLog(btn.tag)
        buttonClickClosure?(btnTagType: MQComposeToolBarButtonTagType(rawValue: btn.tag)!)
    }
  
}
